package com.hnucm.c202001020214;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.c202001020214.object.Goods;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
    public List<Goods.ItemsDTO.ItemDTO> goodsList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView goodsImage;
        TextView goodsName,goodsPrice;



        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsImage = itemView.findViewById(R.id.goodsImage);
            goodsName = itemView.findViewById(R.id.goodsName);
            goodsPrice = itemView.findViewById(R.id.goodsPrice);


        }
    }
    public GoodsAdapter(List<Goods.ItemsDTO.ItemDTO> mGoodsList){
        goodsList = mGoodsList;
    }



    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_goods,parent,false);
        ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position1 = holder.getAdapterPosition();
                Goods.ItemsDTO.ItemDTO itemDTO = goodsList.get(position1);
                Toast.makeText(view.getContext(), "已选择"+itemDTO.title, Toast.LENGTH_SHORT).show();
              //  EventBus.getDefault().post(itemDTO);

            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Goods.ItemsDTO.ItemDTO goods = goodsList.get(position);

        Glide.with(holder.goodsImage).load(goods.getPic_url()).into(holder.goodsImage);

        holder.goodsName.setText(goods.getTitle());
        holder.goodsPrice.setText(goods.getPrice());


    }

    @Override
    public int getItemCount() {
        return 10;
    }





}
